﻿using Fur.DatabaseAccessor;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;

namespace HanXuan.EntityFramework.DbContexts
{
    [AppDbContext("Sqlite3ConnectionString")]
    public class HanXuanDbContext : AppDbContext<HanXuanDbContext>, IMultiTenantOnTable, IModelBuilderFilter
    {
        public HanXuanDbContext(DbContextOptions<HanXuanDbContext> options) : base(options)
        {
        }

        public Guid? GetTenantId()
        {
            return base.Tenant?.TenantId ?? Guid.NewGuid();
        }

        public void OnCreating(ModelBuilder modelBuilder, EntityTypeBuilder entityBuilder, DbContext dbContext, Type dbContextLocator)
        {
            entityBuilder.HasQueryFilter(TenantIdQueryFilterExpression(entityBuilder, GetTenantId().Value));
        }
    }
}
